//
//  VisitorView.m
//  weibo110
//
//  Created by 臧松 on 15/12/26.
//  Copyright © 2015年 臧松. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "VisitorView.h"
#import "UIButton+Extension.h"
#import "UILabel+Extension.h"
#import "UIImageView+Extension.h"
#import "Masonry.h"
@interface VisitorView ()
// 图标
@property (nonatomic,strong) UIImageView * iconView;
// 小房子
@property (nonatomic,strong) UIImageView * homeIconView;
// 消息文字
@property (nonatomic,strong) UILabel * messageLabel;
// 遮罩视图
@property (nonatomic,strong) UIImageView * maskIconView;

@end

@implementation VisitorView
// 懒加载
-(UIImageView *)iconView{
    if (_iconView == nil) {
        _iconView = [UIImageView imageViewWithImageName:@"visitordiscover_feed_image_smallicon"];
        }

    return _iconView;
}

-(UIImageView *)homeIconView{
    if (_homeIconView == nil) {
        _homeIconView = [UIImageView imageViewWithImageName:@"visitordiscover_feed_image_house"];
    }
    return _homeIconView;
}
-(UILabel *)messageLabel{
    if (_messageLabel == nil) {
        _messageLabel = [UILabel labelWithTitle:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜" fontSize:14 color:[UIColor darkGrayColor] screenInset:0];
    }
    return _messageLabel;
}
-(UIButton *)registerButton{
    if (_registerButton == nil) {
        _registerButton = [UIButton btnWithTitle:@"注册" color:[UIColor orangeColor] imageName:@"common_button_white_disable"];
    }
    return  _registerButton;
}
-(UIButton *)loginButton{
    if (_loginButton == nil) {
        _loginButton = [UIButton btnWithTitle:@"登录" color:[UIColor darkGrayColor] imageName:@"common_button_white_disable"];
    }
    return _loginButton;
}
-(UIImageView *)maskIconView{
    if (_maskIconView == nil) {
        _maskIconView =[UIImageView imageViewWithImageName:@"visitordiscover_feed_mask_smallicon"];
    }
    return _maskIconView;
}

// 重写初始化方法设置控件
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}
// 设置视图信息
-(void)initWithImageName:(NSString *)imageName title:(NSString*)title{
   
    self.messageLabel.text = title;
    if (imageName == nil) {
        //开始动画
        [self startAnim];
        return;
    }
    self.homeIconView.hidden = YES;
    self.maskIconView.hidden = YES;
    self.iconView.image = [UIImage imageNamed:imageName];
   
    
}
// 开启动画播放
-(void)startAnim{
    //创建动画
    CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    // 设置动画属性
    anim.toValue = [NSNumber numberWithFloat:2* M_PI];
    anim.repeatCount = MAXFLOAT;
    anim.duration = 20;
    // 用在不断重复的动画上,当动画绑定的图层对应的视图被销毁时,动画会自动被销毁
    anim.removedOnCompletion = NO;
    // 添加到图层
    [self.iconView.layer addAnimation:anim forKey:nil];
    
    
}
// 界面布局
-(void)setupUI{
    // 添加控件
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    // 设置自动布局
    // 图标
    for (UIView * v in self.subviews) {
        v.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
    }];
    // 小房子
   
    [self.homeIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.centerY.equalTo(self.iconView.mas_centerY);
    }];
    
    // 消息文字
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.top.equalTo(self.iconView.mas_bottom).offset(16);
        make.width.equalTo(@224);
        make.height.equalTo(@36);
    }];
    
    //注册按钮

    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.messageLabel.mas_left);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(16);
        make.width.equalTo(@100);
        make.height.equalTo(@36);
    }];
    // 登录按钮
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel.mas_right);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(16);
        make.width.equalTo(@100);
        make.height.equalTo(@36);
    }];
    //遮罩视图
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
//    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.maskIconView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.loginButton attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-100]];
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.loginButton.mas_bottom).offset(-100);
    }];
    
    self.backgroundColor = [UIColor colorWithWhite:237.0/255.0 alpha:1.0];

    
}
@end
